package com.example.player.presentation;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.Manifest;
import android.provider.Settings;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.example.player.R;
import com.example.player.presentation.adaptor.MainViewPagerAdaptor;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
/**
 * 主活动类，绑定ui组件
 * @author 朱文权
 * */
public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tabLayout=findViewById(R.id.tab_layout);
        viewPager=findViewById(R.id.view_page);
        MainViewPagerAdaptor adaptor=new MainViewPagerAdaptor(this);
        viewPager.setAdapter(adaptor);
        new TabLayoutMediator(tabLayout,viewPager,(tabItem,pos)->{
            switch(pos){
                case 0:
                    tabItem.setText("音乐");
                    break;
                case 1:
                    tabItem.setText("歌单");
                    break;
                default:
                    tabItem.setText("未知");
                    break;
            }
        }).attach();
        //检查权限
        requestAudioPermission();
    }
    /**
     * 检查是否有权限
     * */
    private void requestAudioPermission() {
        // 使用临时变量持有launcher引用
        final ActivityResultLauncher<String>[] launcherHolder = new ActivityResultLauncher[1];

        String permission = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ?
                Manifest.permission.READ_MEDIA_AUDIO :
                Manifest.permission.READ_EXTERNAL_STORAGE;

        // 初始化并将launcher存入holder
        launcherHolder[0] = registerForActivityResult(
                new ActivityResultContracts.RequestPermission(),
                granted -> {
                    if (granted) {

                    } else {
                        if (shouldShowRequestPermissionRationale(permission)) {
                            new AlertDialog.Builder(this)
                                    .setTitle("提示")
                                    .setMessage("需要权限才能播放音频")
                                    .setPositiveButton("重试", (d, w) -> launcherHolder[0].launch(permission))
                                    .setNegativeButton("取消", null)
                                    .show();
                        } else {
                            new AlertDialog.Builder(this)
                                    .setTitle("需要设置")
                                    .setMessage("请在系统设置中开启权限")
                                    .setPositiveButton("去设置", (d, w) -> startActivity(new Intent(
                                            Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                            Uri.fromParts("package", getPackageName(), null)
                                    )))
                                    .setNegativeButton("关闭", null)
                                    .show();
                        }
                    }
                });

        // 检查权限状态
        if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {

        } else {
            launcherHolder[0].launch(permission);
        }
    }
}